<template>
  <div class="course-detail">
    <Header />
    <div class="container">
      <div class="content">
        <!-- 课程基本信息 -->
        <div class="course-header">
          <div class="course-info">
            <div class="course-main">
              <h1 class="course-title">{{ course.title }}</h1>
              <p class="course-desc">{{ course.description }}</p>
              <div class="course-meta">
                <span class="meta-item">
                  <i class="fas fa-users"></i>
                  {{ course.studentCount }}人在学
                </span>
                <span class="meta-item">
                  <i class="fas fa-book"></i>
                  共{{ course.lessonCount }}课时
                </span>
                <span class="meta-item">
                  <i class="fas fa-clock"></i>
                  {{ course.duration }}
                </span>
                <span class="meta-item">
                  <i class="fas fa-signal"></i>
                  {{ course.level }}
                </span>
              </div>
            </div>
            <div class="course-action">
              <div class="price-box">
                <template v-if="course.price > 0">
                  <span class="price">¥{{ course.price }}</span>
                  <span class="original-price">¥{{ course.originalPrice }}</span>
                </template>
                <span v-else class="free">限时免费</span>
              </div>
              <button class="buy-btn" @click="handleBuy">立即购买</button>
              <button class="try-btn" @click="handleTry">免费试学</button>
            </div>
          </div>
        </div>

        <div class="course-body">
          <div class="course-content">
            <!-- 课程导航 -->
            <div class="course-nav">
              <a 
                v-for="tab in tabs" 
                :key="tab.key"
                :class="['nav-item', { active: currentTab === tab.key }]"
                @click="currentTab = tab.key"
              >
                {{ tab.text }}
              </a>
            </div>

            <!-- 课程介绍 -->
            <div v-show="currentTab === 'intro'" class="tab-content intro-content">
              <div class="section-title">课程介绍</div>
              <div class="rich-text" v-html="course.introduction"></div>
              
              <div class="section-title">课程收获</div>
              <div class="gains-list">
                <div v-for="(gain, index) in course.gains" :key="index" class="gain-item">
                  <i class="fas fa-check-circle"></i>
                  <span>{{ gain }}</span>
                </div>
              </div>

              <div class="section-title">适合人群</div>
              <div class="suitable-list">
                <div v-for="(item, index) in course.suitableFor" :key="index" class="suitable-item">
                  <i class="fas fa-user-check"></i>
                  <span>{{ item }}</span>
                </div>
              </div>
            </div>

            <!-- 课程大纲 -->
            <div v-show="currentTab === 'outline'" class="tab-content outline-content">
              <div class="chapter-list">
                <div v-for="chapter in course.chapters" :key="chapter.id" class="chapter-item">
                  <div class="chapter-header" @click="toggleChapter(chapter)">
                    <div class="chapter-title-wrapper">
                      <i :class="['fas', chapter.isExpanded ? 'fa-chevron-down' : 'fa-chevron-right']"></i>
                      <h3 class="chapter-title">{{ chapter.title }}</h3>
                    </div>
                    <span class="lesson-count">{{ chapter.lessons.length }}课时</span>
                  </div>
                  <div class="lesson-list" v-show="chapter.isExpanded">
                    <div v-for="lesson in chapter.lessons" :key="lesson.id" class="lesson-item">
                      <div class="lesson-info">
                        <i class="fas fa-play-circle"></i>
                        <span class="lesson-title">{{ lesson.title }}</span>
                        <span v-if="lesson.isFree" class="free-tag">免费</span>
                      </div>
                      <span class="lesson-duration">{{ lesson.duration }}</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <!-- 讲师介绍 -->
            <div v-show="currentTab === 'teacher'" class="tab-content teacher-content">
              <div class="teacher-card">
                <img :src="course.teacher.avatar" :alt="course.teacher.name" class="teacher-avatar">
                <div class="teacher-info">
                  <div class="teacher-header">
                    <h3 class="teacher-name">{{ course.teacher.name }}</h3>
                    <span class="teacher-title">{{ course.teacher.title }}</span>
                  </div>
                  <p class="teacher-desc">{{ course.teacher.description }}</p>
                </div>
              </div>
            </div>
          </div>

          <!-- 相关推荐 -->
          <div class="course-sidebar">
            <div class="sidebar-title">相关课程推荐</div>
            <div class="recommend-list">
              <div 
                v-for="item in recommendCourses" 
                :key="item.id" 
                class="recommend-item"
                @click="goToCourse(item.id)"
              >
                <img :src="item.image" :alt="item.title" class="recommend-image">
                <div class="recommend-info">
                  <h4 class="recommend-title">{{ item.title }}</h4>
                  <div class="recommend-meta">
                    <span>{{ item.studentCount }}人在学</span>
                    <span class="recommend-price" v-if="item.price > 0">¥{{ item.price }}</span>
                    <span class="recommend-price free" v-else>免费</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import Header from '../components/Header.vue'

const router = useRouter()

const currentTab = ref('intro')

const tabs = [
  { key: 'intro', text: '课程介绍' },
  { key: 'outline', text: '课程大纲' },
  { key: 'teacher', text: '讲师介绍' }
]

// 课程数据
const course = ref({
  id: 1,
  title: 'Python零基础入门到实战',
  description: '从零开始学习Python，掌握Python编程基础知识，配套课程资料，理论实践结合，让你快速入门并进阶。',
  studentCount: 1234,
  lessonCount: 48,
  duration: '24小时',
  level: '入门',
  price: 299,
  originalPrice: 399,
  introduction: `
    <p>Python是当下最热门的编程语言之一，被广泛应用于Web开发、人工智能、数据分析等领域。本课程从零基础开始，通过理论与实践相结合的方式，帮助你掌握Python编程的基础知识和核心概念。</p>
    <p>课程特点：</p>
    <ul>
      <li>零基础入门，通俗易懂</li>
      <li>实战案例，边学边练</li>
      <li>配套课程资料，课后复习</li>
      <li>在线答疑，及时解惑</li>
    </ul>
  `,
  gains: [
    '掌握Python基础语法和核心概念',
    '学会使用Python进行基础程序开发',
    '理解面向对象编程思想',
    '掌握常用模块和第三方库的使用',
    '能够独立开发简单的Python应用'
  ],
  suitableFor: [
    '零基础想要学习编程的初学者',
    '想要转行到IT行业的职场人士',
    '对Python编程感兴趣的学生',
    '需要使用Python进行数据分析的从业者'
  ],
  chapters: [
    {
      id: 1,
      title: '第1章：Python基础入门',
      isExpanded: true,
      lessons: [
        {
          id: 1,
          title: 'Python介绍与环境搭建',
          duration: '25:30',
          isFree: true
        },
        {
          id: 2,
          title: 'Python基础语法',
          duration: '35:15',
          isFree: true
        },
        {
          id: 3,
          title: '变量与数据类型',
          duration: '28:45',
          isFree: false
        }
      ]
    },
    {
      id: 2,
      title: '第2章：Python进阶知识',
      isExpanded: false,
      lessons: [
        {
          id: 4,
          title: '函数定义与调用',
          duration: '40:20',
          isFree: false
        },
        {
          id: 5,
          title: '面向对象编程基础',
          duration: '45:10',
          isFree: false
        },
        {
          id: 6,
          title: '模块与包',
          duration: '38:30',
          isFree: false
        }
      ]
    }
  ],
  teacher: {
    name: '张老师',
    title: '高级工程师',
    avatar: 'https://img61.ddimg.cn/2024/3/26/2024032610360944729.jpg',
    description: '从事Python开发工作8年，曾就职于多家知名互联网公司，参与过多个大型项目的开发。教学经验丰富，善于把复杂的概念用简单的方式讲解清楚。'
  },
  courseCover: 'https://img3m0.ddimg.cn/74/31/29668590-1_h_1705747719.jpg'
})

// 推荐课程数据
const recommendCourses = ref([
  {
    id: 2,
    title: 'Java零基础入门',
    image: 'https://img3m2.ddimg.cn/74/31/29668592-1_h_1705747719.jpg',
    studentCount: 856,
    price: 399
  },
  {
    id: 3,
    title: 'Web前端开发实战',
    image: 'https://img3m1.ddimg.cn/64/27/12100020931-1_h_1739000101.jpg',
    studentCount: 1023,
    price: 0
  },
  {
    id: 4,
    title: '大数据分析入门',
    image: 'https://img3m0.ddimg.cn/74/31/29668590-1_h_1705747719.jpg',
    studentCount: 567,
    price: 299
  }
])

// 切换章节展开状态
const toggleChapter = (chapter) => {
  chapter.isExpanded = !chapter.isExpanded
}

// 购买课程
const handleBuy = () => {
  // TODO: 实现购买逻辑
  console.log('购买课程:', course.value.id)
}

// 免费试学
const handleTry = () => {
  router.push(`/study?courseId=${course.value.id}`)
}

// 跳转到课程详情
const goToCourse = (courseId) => {
  router.push(`/course/${courseId}`)
}
</script>

<style scoped>
.course-detail {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-top: 64px;
}

.content {
  background: white;
  border-radius: 8px;
  margin-top: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.course-header {
  background: white;
  padding: 30px;
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.course-info {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 40px;
}

.course-title {
  font-size: 24px;
  color: #333;
  margin: 0 0 16px;
  font-weight: 500;
}

.course-desc {
  font-size: 14px;
  color: #666;
  margin: 0 0 20px;
  line-height: 1.6;
}

.course-meta {
  display: flex;
  gap: 24px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #666;
  font-size: 14px;
}

.meta-item i {
  color: #999;
}

.course-action {
  background: white;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.price-box {
  margin-bottom: 20px;
  text-align: center;
}

.price {
  font-size: 32px;
  color: #ff6b00;
  font-weight: bold;
  margin-right: 8px;
}

.original-price {
  font-size: 16px;
  color: #999;
  text-decoration: line-through;
}

.free {
  font-size: 24px;
  color: #52c41a;
  font-weight: bold;
}

.buy-btn,
.try-btn {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.buy-btn {
  background: #ff6b00;
  color: white;
  margin-bottom: 12px;
}

.buy-btn:hover {
  background: #ff8533;
}

.try-btn {
  background: white;
  color: #ff6b00;
  border: 1px solid #ff6b00;
}

.try-btn:hover {
  background: #fff2e8;
}

.course-body {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 20px;
}

.course-content {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.course-nav {
  display: flex;
  border-bottom: 1px solid #f0f0f0;
}

.nav-item {
  padding: 16px 24px;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
}

.nav-item:hover {
  color: #ff6b00;
}

.nav-item.active {
  color: #ff6b00;
}

.nav-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: #ff6b00;
}

.tab-content {
  padding: 24px;
}

.section-title {
  font-size: 18px;
  color: #333;
  margin: 24px 0 16px;
  font-weight: 500;
}

.section-title:first-child {
  margin-top: 0;
}

.rich-text {
  color: #666;
  line-height: 1.8;
}

.rich-text p {
  margin: 12px 0;
}

.rich-text ul {
  padding-left: 20px;
  margin: 12px 0;
}

.gains-list,
.suitable-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.gain-item,
.suitable-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  color: #666;
}

.gain-item i,
.suitable-item i {
  color: #52c41a;
  margin-top: 4px;
}

.chapter-item {
  border-bottom: 1px solid #f0f0f0;
}

.chapter-item:last-child {
  border-bottom: none;
}

.chapter-header {
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s;
}

.chapter-header:hover {
  background: #f9f9f9;
}

.chapter-title-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chapter-title-wrapper i {
  font-size: 12px;
  color: #999;
  transition: all 0.3s;
}

.chapter-title {
  font-size: 16px;
  color: #333;
  margin: 0;
  font-weight: 500;
}

.lesson-list {
  background: #fafafa;
}

.lesson-item {
  padding: 12px 16px 12px 36px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.lesson-info {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

.lesson-info i {
  color: #999;
  font-size: 14px;
}

.lesson-title {
  font-size: 14px;
  color: #666;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.free-tag {
  padding: 2px 6px;
  background: #f6ffed;
  border: 1px solid #b7eb8f;
  border-radius: 2px;
  color: #52c41a;
  font-size: 12px;
}

.lesson-duration {
  font-size: 12px;
  color: #999;
}

.teacher-card {
  display: flex;
  gap: 20px;
  padding: 24px;
  background: #fafafa;
  border-radius: 8px;
}

.teacher-avatar {
  width: 120px;
  height: 120px;
  border-radius: 8px;
  object-fit: cover;
}

.teacher-info {
  flex: 1;
}

.teacher-header {
  margin-bottom: 12px;
}

.teacher-name {
  font-size: 20px;
  color: #333;
  margin: 0 0 4px;
  font-weight: 500;
}

.teacher-title {
  font-size: 14px;
  color: #666;
}

.teacher-desc {
  font-size: 14px;
  color: #666;
  line-height: 1.8;
  margin: 0;
}

.course-sidebar {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.sidebar-title {
  font-size: 16px;
  color: #333;
  margin: 0 0 16px;
  font-weight: 500;
}

.recommend-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.recommend-item {
  display: flex;
  gap: 12px;
  cursor: pointer;
  transition: all 0.3s;
}

.recommend-item:hover {
  transform: translateY(-2px);
}

.recommend-image {
  width: 120px;
  height: 68px;
  border-radius: 4px;
  object-fit: cover;
}

.recommend-info {
  flex: 1;
  min-width: 0;
}

.recommend-title {
  font-size: 14px;
  color: #333;
  margin: 0 0 8px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.recommend-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #999;
}

.recommend-price {
  color: #ff6b00;
  font-weight: 500;
}

.recommend-price.free {
  color: #52c41a;
}
</style> 